﻿package lobby.exchangeddata
{
	import flash.events.EventDispatcher;
	import lobby.constants.ConstData;
	import lobby.events.LobbyEvent;
	
	/**
	 * ...
	 * @author Bamboo
	 */
	public class LobbyModel extends EventDispatcher
	{
		public var userInfo:LobbyUserInfo;
		public var listCasino:Array;
		public var listTableDetail:Array;
		public var listGameTypeInCasino:Array;
		public var betStatusInfo:BetStatusInfo;
		public var historyInfo:HistoryInfoLobby;
		public var transactionReportInfo:TransactionReportInfo;
		public var transferReportInfo:TransferReportInfo;
		private var welcomeMessageInfo:WelcomeMessage;
		
		public function LobbyModel()
		{
			userInfo = new LobbyUserInfo();
			listCasino = new Array();
			listTableDetail = new Array();
			listGameTypeInCasino = new Array();
			betStatusInfo = new BetStatusInfo();
			historyInfo = new HistoryInfoLobby();
			welcomeMessageInfo = new WelcomeMessage();
			transactionReportInfo = new TransactionReportInfo();
			transferReportInfo = new TransferReportInfo();
			this.betStatusInfo.addEventListener(LobbyEvent.UPDATE_BET_STATUS, hdlPassEvent);
			this.historyInfo.addEventListener(LobbyEvent.UPDATE_HISTORY_DATE, hdlPassEvent);
			this.historyInfo.addEventListener(LobbyEvent.UPDATE_HISTORY, hdlPassEvent);
			this.welcomeMessageInfo.addEventListener(LobbyEvent.UPDATE_WELCOME_MESSAGE, hdlPassEvent);
			
			this.transactionReportInfo.addEventListener(LobbyEvent.UPDATE_DATE_REPORT, hdlPassEvent);
			this.transferReportInfo.addEventListener(LobbyEvent.UPDATE_DATE_REPORT, hdlPassEvent);
			
			this.transactionReportInfo.addEventListener(LobbyEvent.UPDATE_TRANSACTION_REPORT, hdlPassEvent);
			this.transferReportInfo.addEventListener(LobbyEvent.UPDATE_TRANSFER_REPORT, hdlPassEvent);
			this.transactionReportInfo.addEventListener(LobbyEvent.UPDATE_DETAIL_TRANSACTION_REPORT, hdlPassEvent);
		}
		
		private function hdlPassEvent(e:LobbyEvent):void
		{
			dispatchEvent(e);
		}
		
		public function updateSetSessionToken(res:String):void
		{
			userInfo.resultSetSessionToken(res);
		}
		
		public function get getSessionId():String
		{
			return userInfo.getSessionId;
		}
		
		//Business for History Data
		public function initListHistory(result:String):void
		{
			listCasino				= new Array();
			listTableDetail			= new Array();
			listGameTypeInCasino 	= new Array();
			trace("result" + result + "|||||||||");
			var kenoId:String = "11";
			var temp:Array = new Array();
			temp = result.split("#");
			//ctreate list Casino
			listCasino = temp[0].split("$");
			trace(temp[0], temp[1])
			//create list GameType for the casino
			var arrCasino:Array = temp[1].split("$");
			
			for (var j:uint = 0; j < arrCasino.length; j++)
			{
				var arrGameType:Array = arrCasino[j].split("@");
				if (arrGameType.length > 1)
				{
					trace(arrGameType)
					for (var k:uint = 1; k < arrGameType.length; k++)
					{
						if (arrGameType[0] == kenoId)
						{
							break;
						}
						var itemGameType:ItemGameTypeInCasino = new ItemGameTypeInCasino(arrGameType[0], arrGameType[k]);
						listGameTypeInCasino.push(itemGameType);
					}
				}
			}
			
			//create list Table 
			var arrTable:Array = temp[2].split("$");
			var info:HistoryInfoBase;
			for (var i:uint = 0; i < arrTable.length; i++)
			{
				var detailTable:Array = arrTable[i].split("@");
				if (detailTable[0] != kenoId)
				{
					switch (detailTable[5])
					{
						case ConstData.GAME_TYPE_BAC: 
							info = new HistoryInfoBaccarat();
							break;
						case ConstData.GAME_TYPE_BAC_IN: 
							info = new HistoryInfoBaccarat();
							break;
						case ConstData.GAME_TYPE_FANTAN: 
							info = new HistoryInfoFantan();
							break;
						case ConstData.GAME_TYPE_DT: 
							info = new HistoryInfoDragonTiger();
							break;
						case ConstData.GAME_TYPE_SICBO: 
							info = new HistoryInfoSicbo();
							break;
						case ConstData.GAME_TYPE_SEDIE: 
							info = new HistoryInfoSedie();
							break;
						case ConstData.GAME_TYPE_ROULETTE: 
							info = new HistoryInfoRoulette();
							break;
					}
					info.init(detailTable[0], detailTable[1], detailTable[5], detailTable[2], detailTable[3], detailTable[4].toLowerCase() == "false");
					info.addEventListener(LobbyEvent.UPDATE_HISTORY_ROUTE, hldUpdateHistory);
					listTableDetail.push(info);
				}
			}
			// sort historyInfo on gameTypeIndex and tableId
			listTableDetail.sortOn(["getGameTypeIndex", "getTableId"], [Array.NUMERIC, Array.CASEINSENSITIVE]);
			
			var evt:LobbyEvent = new LobbyEvent(LobbyEvent.INIT_HISTORY_COMPLETE, true);
			dispatchEvent(evt);
			
			//test		
			var typeGame:Array = getGameTypeInCasino("2");
			trace("=============all game type");
			var typeGame1:Array = getGameTypeInCasino();
			trace("=============casino 2");
			var tableCasino:Array = getTablesCasino("2");
			tableCasino.forEach(traceCasino);
			trace("============= all");
			var tableCasino1:Array = getTablesCasino();
			tableCasino1.forEach(traceCasino);
			trace("===================casino12, ba");
			var listTableGameType:Array = getTablesInGameTypeInCasion("Bi", "2");
			listTableGameType.forEach(traceCasino);
			trace("===================game type ba");
			var listTableGameType1:Array = getTablesInGameTypeInCasion("Ba");
			listTableGameType1.forEach(traceCasino)
			trace("===================");
			var obj:HistoryInfoBase = HistoryInfoBase(getTableElement("6"));
			trace(obj.getCasinoName, obj.getGameType, obj.getTableClose);
		}
		
		/**
		 * @usage	Filter tables in the casino that has casinoId = casinoId, if casinoid = null return all table of casinos
		 * @param	casinoId
		 * @return
		 */
		
		public function getTablesCasino(casinoId:String = null):Array
		{
			function isCasino(element:HistoryInfoBase, index:int, arr:Array):Boolean
			{
				return (element.getCasinoId == casinoId);
			}
			if (casinoId == null)
			{
				return listTableDetail;
			}
			var tablesList:Array = listTableDetail.filter(isCasino);
			return tablesList;
		}
		
		/**
		 * @usage	Filter tables in the casino that has casinoId = casinoId and gameType = gameType, if casinoId=null, return tables in game type
		 * @param	casinoId
		 * @param	gameType
		 * @param	listHistory
		 * @return
		 */
		public function getTablesInGameTypeInCasion(gameType:String, casinoId:String = null):Array
		{
			
			function isGameTypeInCasino(element:HistoryInfoBase, index:int, arr:Array):Boolean
			{
				if (casinoId == null)
				{
					return (element.getGameType == gameType);
				}
				return (element.getCasinoId == casinoId && element.getGameType == gameType);
			}
			var listTable:Array = listTableDetail.filter(isGameTypeInCasino);
			//trace(listTable)
			return listTable;
		}
		
		/**
		 * @usage	Filter table in listHistory that has tableId = tableId
		 * @param	tableId
		 * @param	listHistory
		 * @return
		 */
		public function getTableElement(tableId:String):HistoryInfoBase
		{
			function isTable(element:HistoryInfoBase, index:int, arr:Array):Boolean
			{
				return (element.getTableId == tableId);
			}
			var temp:Array = listTableDetail.filter(isTable);
			if (temp.length != 0)
			{
				trace("leng", temp.length);
				trace(temp[0].getTableId);
				var obj:HistoryInfoBase = new HistoryInfoBase();
				obj = temp[0];
				return obj;
			}
			return null;
		}
		
		/**
		 * @usage	Filter typeGame in Casino, if casino = null return all type.
		 * @param	casinoId
		 * @return
		 */
		public function getGameTypeInCasino(casinoId:String = null):Array
		{
			function isGameType(element:ItemGameTypeInCasino, index:int, arr:Array):Boolean
			{
				return (element.getCasinoId == casinoId);
			}
			var listTypeGame:Array = new Array();
			if (casinoId == null)
			{
				trace("why");
				
				for (var i:uint = 0; i < listGameTypeInCasino.length; i++)
				{
					var temp:ItemGameTypeInCasino = listGameTypeInCasino[i];
					if (!checkGameType(temp.getGameType, listTypeGame))
						listTypeGame.push(listGameTypeInCasino[i]);
				}
				listTypeGame.forEach(traceGameType);
				return listTypeGame;
			}
			
			listTypeGame = listGameTypeInCasino.filter(isGameType);
			listTypeGame.forEach(traceGameType);
			
			return listTypeGame;
		}
		
		private function checkGameType(value:String, gameType:Array):Boolean
		{
			for (var i:uint = 0; i < gameType.length; i++)
			{
				var temp:ItemGameTypeInCasino = gameType[i];
				if (value == temp.getGameType)
				{
					return true;
				}
			}
			return false;
		}
		
		public function traceCasino(element:*, index:int, arr:Array):void
		{
			trace("\t" + element.getCasinoId + ":" + (element.getCasinoName) + ":" + element.getGameType + ":" + element.getTableId);
		}
		
		public function traceGameType(element:ItemGameTypeInCasino, index:int, arr:Array):void
		{
			trace("\t" + element.getCasinoId + ":" + element.getGameType);
		}
		
		// End Business for History Data
		
		private function hldUpdateHistory(e:LobbyEvent):void
		{
			trace("hldUpdateHistory");
			var hisInfo:HistoryInfoBase = HistoryInfoBase(getTableElement(e.tableId));
			if (hisInfo == null)
				return;
			var evt:LobbyEvent = new LobbyEvent(LobbyEvent.UPDATE_HISTORY_VIEW, true);
			evt.tableId = hisInfo.getTableId;
			evt.dataHis = hisInfo.getHistory;
			dispatchEvent(evt);
			
		}
		
		public function setHistory(result:String):void
		{
			
			var tableId:String = uint(result.substr(ConstData.LEN_SUCCESS_RESULT, ConstData.LEN_TABLE_ID)).toString();
			trace("setHistory:" + tableId)
			var hisInfo:HistoryInfoBase = HistoryInfoBase(getTableElement(tableId));
			if (hisInfo == null)	
				return;
			if (hisInfo.odlResult == result)
				return;
			hisInfo.setHistory(hisInfo.returnSetHistory(result));
		
		}
		
		public function updateRecentBet(res:String):void
		{
			betStatusInfo.resultRecentBet(res);
		}
		
		public function updateHistoryDate(res:String):void
		{
			historyInfo.resultServerDateTime(res);
		}
		
		public function updateHistory(res:String):void
		{
			historyInfo.resultMemberWinLose(res);
		}
		
		public function updateWelComeMessage(res:String):void
		{
			welcomeMessageInfo.setMessage(res)
		}
		
		public function get getWelcomeMessageInfo():String
		{
			return welcomeMessageInfo.strMsg;
		}
		
		public function updateHistoryByCancelGame(tableId):void
		{
			var hisInfo:HistoryInfoBase = HistoryInfoBase(getTableElement(tableId));
			if (hisInfo == null)
				return;
			hisInfo.setOdlResult = "";
		}
	}

}